import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  },
  {
    path: '/',
    name: 'home',
    component: () => import('../views/HomeView.vue'),
    children: [
      {
        path: '/userlist',
        name: 'userlist',
        component: () => import('../components/YonghuGuanli/UserList.vue')
      },
      {
        path: '/jiaoselist',
        name: 'jiaoselist',
        component: () => import('../components/QuanxianGuanli/JiaoseList.vue')
      },
      {
        path: '/quanxianlist',
        name: 'quanxianlist',
        component: () => import('../components/QuanxianGuanli/QuanxianList.vue')
      },
      {
        path: '/goodslist',
        name: 'goodslist',
        component: () => import('../components/ShangpinGuanli/GoodsList.vue')
      },
      {
        path: '/gooodsort',
        name: 'gooodsort',
        component: () => import('../components/ShangpinGuanli/GoodsSort.vue')
      },
      {
        path: '/sortnum',
        name: 'sortnum',
        component: () => import('../components/ShangpinGuanli/SortNum.vue')
      },
      {
        path: '/dingdanlist',
        name: 'dingdanlist',
        component: () => import('../components/DingdanGuanli/DingdanList.vue')
      },
      {
        path: '/numtable',
        name: 'numtable',
        component: () => import('../components/ShujuTongji/NumTable.vue')
      },
    ]
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 前置路由守卫 
router.beforeEach((to, from, next) => {
  const tokenStr = window.sessionStorage.getItem("token")
  if (to.path == '/login') {
    next();
  } else {
    if (!tokenStr) {
      return next("/login")
    } else {
      next();
    }
  }

})

export default router
